﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace WellDone
{
    public partial class LoadScheduleTemplateDialog : ChildWindow
    {
        PeriodEditor peParent;
        int currentEmployeeID = 0;
        DateTime startDate;

        public LoadScheduleTemplateDialog()
        {
            InitializeComponent();
        }

        private async void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.IsEnabled = false;
            // Load the schedule of the selected template
            if (cmbScheduleTemplate.Items.Count > 0)
            {
                // Clear the schedule for this user for this week, if directed to do so
                if (cbClearSchedule.IsChecked==true)
                {
                    await Globals.db.GetScheduleObjects("EmployeeID=" + currentEmployeeID+" and Start between '" + startDate.ToShortDateString()+
                        "' and '"+startDate.AddDays(Globals.k_int_days_in_week).ToShortDateString()+"' and RecurrenceType=0 and Preference=0");
                    foreach (ServiceReferenceOpenRPOS.Schedule sch in Globals.db.ScheduleRecords)
                    {
                        await Globals.db.DeleteScheduleObject(sch);
                    }
                }
                // Get the schedule template records
                await Globals.db.GetScheduleObjects("ScheduleTemplateID=" + Globals.db.ScheduleTemplateRecords[cmbScheduleTemplate.SelectedIndex].col_ID);
                foreach (ServiceReferenceOpenRPOS.Schedule sch in Globals.db.ScheduleRecords)
                {
                    // Determine the exact day to apply this record...
                    int dayDiff = (int)sch.col_Start.DayOfWeek - (int)startDate.DayOfWeek;
                    sch.col_Start = startDate.AddDays(dayDiff).Date + sch.col_Start.TimeOfDay;
                    sch.col_Stop = startDate.AddDays(dayDiff).Date + sch.col_Stop.TimeOfDay;
                    sch.col_ScheduleTemplateID = 0;
                    sch.col_EmployeeID = currentEmployeeID;
                    sch.col_RecurrenceType = 0;
                    sch.col_RoleID = (peParent.sc0.EmployeeRoleRecords.Count > 0) ? peParent.sc0.EmployeeRoleRecords[0].col_RoleID : 0;
                    sch.col_ID = 0;
                    sch.col_Preference = false;
                    sch.bFilterEmployeeID = true;
                    sch.bFilterPreference = true;
                    sch.bFilterRecurrenceType = true;
                    sch.bFilterRoleID = true;
                    sch.bFilterScheduleTemplateID = true;
                    sch.bFilterStart = true;
                    sch.bFilterStop = true;
                    sch.col_ID = await Globals.db.AddScheduleObject(sch);
                }
            }
            // Re-load the parent
            Globals.schedule.refreshData(true);
            peParent.init(currentEmployeeID, startDate);
            this.IsEnabled = true;
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public async Task<int> loadScheduleTemplates(PeriodEditor pe, int employeeID, DateTime selectedDate)
        {
            this.IsEnabled = false;
            peParent = pe;
            currentEmployeeID = employeeID;
            startDate = selectedDate;
            // Get the templates...
            await Globals.db.GetScheduleTemplateObjects("1=1 order by Name");
            cmbScheduleTemplate.Items.Clear();
            foreach (ServiceReferenceOpenRPOS.ScheduleTemplate st in Globals.db.ScheduleTemplateRecords)
            {
                cmbScheduleTemplate.Items.Add(st.col_Name);
            }
            if (cmbScheduleTemplate.Items.Count > 0)
            {
                cmbScheduleTemplate.SelectedIndex = 0;
            }
            this.IsEnabled = true;
            return cmbScheduleTemplate.Items.Count;
        }

    }
}

